SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Usuario]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Usuario](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nombre] [varchar](50) NOT NULL,
	[Apellidos] [varchar](150) NOT NULL,
	[Login] [varchar](15) NOT NULL,
	[Password] [varchar](15) NOT NULL,
	[Email] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Usuario_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CategoriaLogopeda]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[CategoriaLogopeda](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nombre] [varchar](50) NOT NULL,
 CONSTRAINT [PK_CategoriaLogopeda_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Consulta]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Consulta](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Texto] [nvarchar](4000) NOT NULL,
	[Respondida] [bit] NOT NULL,
	[Fecha] [datetime] NOT NULL,
	[UsuarioId] [int] NOT NULL,
 CONSTRAINT [PK_Consulta] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Logopeda]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Logopeda](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Nombre] [varchar](50) NOT NULL,
	[Apellidos] [varchar](150) NOT NULL,
	[Login] [varchar](15) NOT NULL,
	[Password] [varchar](15) NOT NULL,
	[Email] [varchar](50) NOT NULL,
	[Ciudad] [varchar](150) NOT NULL,
	[Colegiado] [bit] NOT NULL,
	[Direccion] [varchar](150) NULL,
	[LugarTrabajo] [varchar](150) NULL,
	[Poblacion] [varchar](150) NULL,
	[Telefono] [varchar](9) NULL,
	[CategoriaId] [int] NOT NULL,
 CONSTRAINT [PK_Logopeda_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Respuesta]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Respuesta](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Texto] [nvarchar](4000) NOT NULL,
	[Fecha] [datetime] NOT NULL,
	[LogopedaId] [int] NOT NULL,
	[ConsultaId] [int] NOT NULL,
 CONSTRAINT [PK_Respuesta_1] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Consulta_Usuario]') AND parent_object_id = OBJECT_ID(N'[dbo].[Consulta]'))
ALTER TABLE [dbo].[Consulta]  WITH CHECK ADD  CONSTRAINT [FK_Consulta_Usuario] FOREIGN KEY([UsuarioId])
REFERENCES [dbo].[Usuario] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Logopeda_Logopeda]') AND parent_object_id = OBJECT_ID(N'[dbo].[Logopeda]'))
ALTER TABLE [dbo].[Logopeda]  WITH CHECK ADD  CONSTRAINT [FK_Logopeda_Logopeda] FOREIGN KEY([CategoriaId])
REFERENCES [dbo].[CategoriaLogopeda] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Respuesta_Consulta]') AND parent_object_id = OBJECT_ID(N'[dbo].[Respuesta]'))
ALTER TABLE [dbo].[Respuesta]  WITH CHECK ADD  CONSTRAINT [FK_Respuesta_Consulta] FOREIGN KEY([ConsultaId])
REFERENCES [dbo].[Consulta] ([Id])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Respuesta_Logopeda]') AND parent_object_id = OBJECT_ID(N'[dbo].[Respuesta]'))
ALTER TABLE [dbo].[Respuesta]  WITH CHECK ADD  CONSTRAINT [FK_Respuesta_Logopeda] FOREIGN KEY([LogopedaId])
REFERENCES [dbo].[Logopeda] ([Id])
